package com.simple.microservices.service.active;

import cn.dev33.satoken.stp.StpUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.simple.microservices.api.active.BlindBoxService;
import com.simple.microservices.api.dto.MySpotDTO;
import com.simple.microservices.commons.exceptions.BusinessException;
import com.simple.microservices.commons.utils.Response;
import com.simple.microservices.dao.dataobject.Banner;
import com.simple.microservices.dao.dataobject.MineSpot;
import com.simple.microservices.dao.dataobject.Spot;
import com.simple.microservices.dao.dataobject.SpotPool;
import com.simple.microservices.dao.mapper.BannerMapper;
import com.simple.microservices.dao.mapper.MineSpotMapper;
import com.simple.microservices.dao.mapper.SpotMapper;
import com.simple.microservices.dao.mapper.SpotPoolMapper;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.Collections;
import java.util.List;
import java.util.stream.Collectors;

@Slf4j
@Service
public class BlindBoxServiceImpl implements BlindBoxService {

    @Resource
    private BannerMapper bannerMapper;
    @Resource
    private MineSpotMapper mineSpotMapper;
    @Resource
    private SpotPoolMapper spotPoolMapper;
    @Resource
    private SpotMapper spotMapper;

    @Autowired
    @Qualifier("redis")
    private RedisTemplate redis;
    @Override
    public List<Banner> getAll() {
        List<Banner>  bannerList = bannerMapper.selectList(new QueryWrapper<>());
        return bannerList;
    }

    @Override
    public SpotPool getSpot(Long userId, String sex) {
        LambdaQueryWrapper<MineSpot> mineSpotLambdaQueryWrapper = new LambdaQueryWrapper<>();
        mineSpotLambdaQueryWrapper.eq(MineSpot::getUserId,userId);
        List<MineSpot> mineSpotList = mineSpotMapper.selectList(mineSpotLambdaQueryWrapper);
        List<Long> spotPoolList = mineSpotList.stream().map(MineSpot::getId).collect(Collectors.toList());

        if (mineSpotList!=null&&mineSpotList.size()>0){
            LambdaQueryWrapper<SpotPool> spotPoolLambdaQueryWrapper = new LambdaQueryWrapper<>();
            spotPoolLambdaQueryWrapper.notIn(SpotPool::getId,spotPoolList);
            spotPoolLambdaQueryWrapper.eq(SpotPool::getSexCode,sex);
            SpotPool spotPool =  spotPoolMapper.selectOne(spotPoolLambdaQueryWrapper);
            if (spotPool==null){
                throw new BusinessException("纸条被取完了");
            }
            return spotPool;

        }else {
            LambdaQueryWrapper<SpotPool> spotPoolLambdaQueryWrapper = new LambdaQueryWrapper<>();
            spotPoolLambdaQueryWrapper.eq(SpotPool::getSex,sex);
            SpotPool spotPool =  spotPoolMapper.selectOne(spotPoolLambdaQueryWrapper);
            return spotPool;

        }
    }

    @Override
    public void saveSpot(SpotPool spotPool) {
        LambdaQueryWrapper<SpotPool> spotPoolLambdaQueryWrapper = new LambdaQueryWrapper<>();
        spotPoolLambdaQueryWrapper.eq(SpotPool::getWechatNumber,spotPool.getWechatNumber());
        SpotPool spotPoolAlready = spotPoolMapper.selectOne(spotPoolLambdaQueryWrapper);
        if (spotPoolAlready==null){
            spotPoolMapper.insert(spotPool);
        }else {
            throw new BusinessException("该微信已存在");
        }
    }

    @Override
    public void saveSpot(Spot spot) {
        LambdaQueryWrapper<Spot> spotLambdaQueryWrapper = new LambdaQueryWrapper<>();
        spotLambdaQueryWrapper.eq(Spot::getUserId,spot.getUserId());
        Spot ret = spotMapper.selectOne(spotLambdaQueryWrapper);
        if (ret==null){
            spotMapper.insert(spot);
        }else {
            throw new BusinessException("已存入");
        }
    }

    @Override
    public void putSpotByUserId(Long userId) {
        LambdaQueryWrapper<Spot> spotLambdaQueryWrapper = new LambdaQueryWrapper<>();
        spotLambdaQueryWrapper.eq(Spot::getUserId,userId);
        Spot spot = spotMapper.selectOne(spotLambdaQueryWrapper);
        if (spot!=null){
            LambdaQueryWrapper<SpotPool> spotPoolLambdaQueryWrapper = new LambdaQueryWrapper<>();
            spotPoolLambdaQueryWrapper.eq(SpotPool::getUserId,userId);
            SpotPool spl = spotPoolMapper.selectOne(spotPoolLambdaQueryWrapper);
            if (spl==null){
                SpotPool spotPool = new SpotPool();
                spotPool.setSex(spot.getSex());
                spotPool.setUserId(spot.getUserId());
                spotPool.setSexCode(spot.getSexCode());
                spotPool.setWechatNumber(spot.getWechatNumber());
                spotPoolMapper.insert(spotPool);
                MineSpot mineSpot = new MineSpot();
                mineSpot.setSpotPoolId(spotPool.getId());
                mineSpot.setSex(spot.getSex());
                mineSpot.setUserId(userId);
                mineSpot.setWechatNumber(spot.getWechatNumber());
                mineSpotMapper.insert(mineSpot);
            }
        }
    }

    @Override
    public Boolean getByUserId(Long userId) {
        log.info("获取字条是否已存在 userId={}",userId);
        Boolean flag = false;
        if (userId==null){
            throw new BusinessException("异常信息");
        }
        LambdaQueryWrapper<Spot> spotLambdaQueryWrapper = new LambdaQueryWrapper<>();
        spotLambdaQueryWrapper.eq(Spot::getUserId,userId);
        Spot spot = spotMapper.selectOne(spotLambdaQueryWrapper);
        if (spot!=null){
            flag=true;
        }
        return flag;
    }

    @Override
    public Boolean getByUserId() {
//        Object tkObj = redis.opsForValue().get(String.format(USER_TOKEN_KEY, token));
        Long userId = StpUtil.getLoginIdAsLong();
//        if (ObjectUtils.isEmpty(tkObj)){
//            Token tk = (Token) tkObj;
//            userId = tk.getSubjectId();
//        }
        Boolean flag = false;
        if (userId==null){
            throw new BusinessException("异常信息");
        }
        LambdaQueryWrapper<Spot> spotLambdaQueryWrapper = new LambdaQueryWrapper<>();
        spotLambdaQueryWrapper.eq(Spot::getUserId,userId);
        Spot spot = spotMapper.selectOne(spotLambdaQueryWrapper);
        if (spot!=null){
            flag=true;
        }
        return flag;
    }

    @Override
    public IPage<MineSpot> getPageList(MySpotDTO convert) {
        if ( convert.getIsLastPage()){
            return new Page<MineSpot>();
        }
        LambdaQueryWrapper<MineSpot> mineSpotLambdaQueryWrapper = new LambdaQueryWrapper<>();
        Long loginId = StpUtil.getLoginIdAsLong();
        mineSpotLambdaQueryWrapper.eq(MineSpot::getUserId,loginId);
        IPage<MineSpot> mineSpotIPage = mineSpotMapper.selectPage(new Page<MineSpot>(convert.getPage(), 20),mineSpotLambdaQueryWrapper);
        return mineSpotIPage;
    }

    @Override
    public List<MineSpot> getAllList() {


        List<MineSpot> list = mineSpotMapper.selectList(new QueryWrapper<>());
        return list;
    }
}
